package com.atguigu.bigdata.scala.mytest.chapter04

object cycle {
    def main(args: Array[String]): Unit = {
      println("==========左闭右开==========")
      for (i <- Range(1,5)){
        println("i = " + i)
      }

      println("==========字符串可以当成char数组遍历==========")
      var s="abc"
      for (c <- s){
        println(c)
      }

      println("==========左闭右开==========")
      for (i <- 1 until 5){
        println(i)
      }

      println("==========左闭右闭==========")
      for (i <- 1 to 5){
        println(i)
      }

      println("==========判断条件成立才可执行循环==========")
      for(i <- Range(1,5) if i!=3){
        println(i)
      }
      println("==========右开,以2的步长增==========")
      for(i <- Range(1,5,2)){
        println(i)
      }

      println("==========右闭,以2的步长增==========")
      for(i <- 1 to 5 by 2){
        println(i)
      }

      println("==========右开,以2的步长减==========")
      for(i <- Range(5,1,-2)){
        println(i)
      }

      println("==========右闭,以2的步长减==========")
      for(i <- 5 to 1 by -2){
        println(i)
      }

      println("==========嵌套循环1==========")
      for(i <- Range(1,5); j <- Range(1,3)){
        println("i="+i+",j="+j)
      }

      println("==========嵌套循环2==========")
      for(i <- Range(1,5)){
        for (j <- Range(1,3)){
          println("i="+i+",j="+j)
        }
      }

      println("==========引入变量==========")
      for(i <- Range(1,5);j=i-1){
        println(j)
      }

      println("==========循环的返回值默认为unit,加yield保留每次循环结果==========")
      val result=for(i <- Range(1,5))yield{
        i*2
      }
      println(result)

      println("==========while循环==========")
      var a=0
      while (a<4){
        println("a"+a)
        a+=1
      }

      println("==========do while循环==========")
      var b=0
      do{
        println("b"+b)
        b+=1
      }while(b<4)

      println("==========循环中断==========")
      scala.util.control.Breaks.breakable {
        for(i <- Range(1,5)){
          if(i==3){
            scala.util.control.Breaks.break
          }
          println(i)
        }
      }



    }
}
